package yunquan.wang.modules.base.web;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import yunquan.wang.base.dao.PropertyFilter;
import yunquan.wang.base.web.WebConstants;
import yunquan.wang.base.web.WebHelper;
import yunquan.wang.base.web.ajax.AjaxDone;
import yunquan.wang.modules.base.entity.Authority;
import yunquan.wang.modules.base.entity.Rolesinfo;
import yunquan.wang.modules.base.service.AuthorityService;
import yunquan.wang.modules.base.service.RolesinfoService;
import yunquan.wang.modules.base.service.RulesInfoService;

@Controller
@RequestMapping("/core/rolesinfo")
public class RolesinfoController {
	@Resource
	private RolesinfoService service;

	@Resource
	private AuthorityService authorityService;
	@Resource 
	private RulesInfoService rulesInfoService;

	@RequestMapping(value = "/list.do")
	public String list(HttpServletRequest request, Model model) {
		Pageable page = WebHelper.getPage(request);
		List<PropertyFilter> filters = WebHelper.filterRequest(request);
		model.addAttribute(WebConstants.ATTRIBUTE_PAGE,
				service.findPage(page, filters));
		return "core/rolesinfoList";
	}

	@RequestMapping(value = "/save.do")
  public @ResponseBody  AjaxDone save(@Valid Rolesinfo entity, BindingResult bindingResult ) {
		AjaxDone done=WebHelper.saveSuccess();
		if (bindingResult.hasErrors()) {
			done= WebHelper.validFail(bindingResult);
		} else {
			try {
				entity = service.save(entity);
			} catch (Exception e) {
			return	WebHelper.saveFail( e);
			}
		}
			done.setCallbackType(AjaxDone.CB_FORWARD);
			done.setForwardUrl("core/rolesinfo/auth.do?id="+entity.getId());
	     return done;
	}
	/**
	 * 为角色设置权限
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/auth.do")
	public String editAuth(String id,Model model)
	{
		Rolesinfo entity=service.get(id);
	     List<Authority> authList=authorityService.findAll();
	     model.addAttribute("allAuthority", authList);
	     model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, entity);
	     model.addAttribute("allRules", rulesInfoService.findAll());
	 return    "core/rolesinfoAuth";
	}
	
	@RequestMapping(value = "/authsave.do")
	public @ResponseBody
	AjaxDone authSave(HttpServletRequest request,String roleId)
	{
		String authId[]=request.getParameterValues("authId");
		Map<String,Set<String>> authAndRules=new HashMap<String, Set<String>>();
		for(String auth:authId)
		{
			Set<String> rulesSet=new HashSet<String>();
			String [] rules=request.getParameterValues("authRule_"+auth);
		for(String rule:rules)
		{
			rulesSet.add(rule);
		}
		authAndRules.put(auth, rulesSet);
		}
		service.saveAuthAndRules(roleId,authAndRules);
		
		return WebHelper.saveSuccess();
	}
	
	
	@RequestMapping(value = "/view.do")
	public String view(String id, Model model) {
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, service.get(id));
		return "core/rolesinfo/view";
	}

	@RequestMapping(value = "/input.do")
	public String input(String id, Model model) {
		Rolesinfo entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = service.get(id);
		} else {
			entity = new Rolesinfo();
		}
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, entity);
		return "core/rolesinfoInput";
	}

	@RequestMapping(value = "/delete.do")
	public @ResponseBody
	AjaxDone delete(String[] id) {
		for (String ids : id) {
			service.delete(ids);
		}
		return WebHelper.deleteSuccess();
	}

}
